package com.stylefeng.guns.modular.ds.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.stylefeng.guns.common.constant.factory.PageFactory;
import com.stylefeng.guns.common.constant.state.ChannelInfoStatus;
import com.stylefeng.guns.common.constant.state.ChannelLevel;
import com.stylefeng.guns.common.constant.state.ChannelStatus;
import com.stylefeng.guns.common.constant.state.EnumCommon;
import com.stylefeng.guns.common.persistence.dao.ChannelInfoMapper;
import com.stylefeng.guns.common.persistence.model.ChannelInfo;
import com.stylefeng.guns.core.base.controller.BaseController;
import com.stylefeng.guns.core.log.LogObjectHolder;
import com.stylefeng.guns.core.mutidatasource.annotion.DataSource;
import com.stylefeng.guns.modular.ds.service.IChannelInfoService;
import com.stylefeng.guns.modular.ds.warpper.ChannelInfoWarpper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 多数据源Demo控制器
 *
 * @author fengshuonan
 * @Date 2018-06-17 18:18:53
 */
@Controller
@RequestMapping("/channelInfo")
public class ChannelInfoController extends BaseController {

    private String PREFIX = "/ds/channelInfo/";

    @Autowired
    private IChannelInfoService channelInfoService;

    @Resource
    private ChannelInfoMapper channelInfoMapper;

    /**
     * 跳转到多数据源Demo首页
     */
    @RequestMapping("")
    @DataSource(name = "dataSourceDemo")
    public String index(Model model) {
        model.addAttribute("statuses", EnumCommon.toMap(ChannelStatus.class));
        model.addAttribute("levels", EnumCommon.toMap(ChannelLevel.class));
        return PREFIX + "channelInfo.html";
    }

    /**
     * 跳转到添加多数据源Demo
     */
    @RequestMapping("/channelInfo_add")
    @DataSource(name = "dataSourceDemo")
    public String channelInfoAdd(Model model) {
        model.addAttribute("statuses", EnumCommon.toMap(ChannelStatus.class));
        model.addAttribute("levels", EnumCommon.toMap(ChannelLevel.class));

        return PREFIX + "channelInfo_add.html";
    }

    /**
     * 跳转到修改多数据源Demo
     */
    @RequestMapping("/channelInfo_update/{channelInfoId}")
    @DataSource(name = "dataSourceDemo")
    public String channelInfoUpdate(@PathVariable Integer channelInfoId, Model model) {
        model.addAttribute("statuses", EnumCommon.toMap(ChannelStatus.class));
        model.addAttribute("levels", EnumCommon.toMap(ChannelLevel.class));


        ChannelInfo channelInfo = channelInfoService.selectById(channelInfoId);
        model.addAttribute("item", channelInfo);
        LogObjectHolder.me().set(channelInfo);
        return PREFIX + "channelInfo_edit.html";
    }

    /**
     * 获取多数据源Demo列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    @DataSource(name = "dataSourceDemo")
    public Object list(@RequestParam(required = false) String condition,
                       @RequestParam(name = "status[]", required = false) String[] status,
                       @RequestParam(name = "level[]", required = false) String[] level) {
        Page<ChannelInfo> page = new PageFactory<ChannelInfo>().defaultPage();
        List<Map<String, Object>> result = channelInfoMapper.list(page, condition, status,level, page.getOrderByField(), page.isAsc());
        page.setRecords((List<ChannelInfo>) new ChannelInfoWarpper(result).warp());
        return super.packForBT(page);
    }

    /**
     * 新增多数据源Demo
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    @DataSource(name = "dataSourceDemo")
    public Object add(ChannelInfo channelInfo) {
        channelInfo.setCreateTime(new Date());
        channelInfo.setUpdateTime(new Date());
        channelInfoService.insert(channelInfo);
        return SUCCESS_TIP;
    }

    /**
     * 删除多数据源Demo
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    @DataSource(name = "dataSourceDemo")
    public Object delete(@RequestParam Integer channelInfoId) {
        channelInfoService.deleteById(channelInfoId);
        return SUCCESS_TIP;
    }

    /**
     * 修改多数据源Demo
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    @DataSource(name = "dataSourceDemo")
    public Object update(ChannelInfo channelInfo) {
        channelInfo.setUpdateTime(new Date());
        channelInfoService.updateById(channelInfo);
        return SUCCESS_TIP;
    }

    /**
     * 多数据源Demo详情
     */
    @RequestMapping(value = "/detail/{channelInfoId}")
    @ResponseBody
    @DataSource(name = "dataSourceDemo")
    public Object detail(@PathVariable("channelInfoId") Integer channelInfoId) {
        return channelInfoService.selectById(channelInfoId);
    }
    /**
     * 启用搜索关键字
     */
    @RequestMapping(value = "/enable")
    @ResponseBody
    @DataSource(name = "dataSourceDemo")
    public Object enable(@RequestParam(value = "channelInfoIds[]") Integer[] channelInfoIds) {
        if (channelInfoIds.length > 0) {
            for (Integer id : channelInfoIds) {
                ChannelInfo channelInfo = channelInfoMapper.selectById(id);
                channelInfo.setStatus(ChannelInfoStatus.ENABLE.getKey());
                channelInfoMapper.updateById(channelInfo);
            }
        }
        return SUCCESS_TIP;
    }

    /**
     * 停用搜索关键字
     */
    @RequestMapping(value = "/disable")
    @ResponseBody
    @DataSource(name = "dataSourceDemo")
    public Object disable(@RequestParam(value = "channelInfoIds[]") Integer[] channelInfoIds) {
        if (channelInfoIds.length > 0) {
            for (Integer id : channelInfoIds) {
                ChannelInfo channelInfo = channelInfoMapper.selectById(id);
                channelInfo.setStatus(ChannelStatus.DISABLE.getKey());
                channelInfoMapper.updateById(channelInfo);
            }
        }
        return SUCCESS_TIP;
    }
}
